﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class CanalStateForm : Form
    {
        tb_CanalState canalState;

        int canalID, regID, avpID, kv, year;

        public CanalStateForm()
        {
            InitializeComponent();
        }

        private void CanalStateForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }
            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());
            year = Convert.ToInt32(cbYear.Text);
            for (int i = 0; i < 4; i++)
            {
                cbKvartal.Items.Add((i + 1).ToString() + " Квартал");
            }
            cbKvartal.SelectedIndex = 0;
        }

        private void ShowCanalState()
        {
            canalState = DBClass.CanalState
                .Where(wh => wh.CanalID == canalID && wh.Date == new DateTime(year, (kv * 3 + 1), 1))
                .SingleOrDefault();

            if (canalState != null)
            {
                this.tbWeirQuantity.Text = canalState.WeirQuantity.ToString();
                this.tbWeirNeed.Text = canalState.WeirNeed.ToString();
                this.tbSANIIRIQuantity.Text = canalState.SANIIRIQuantity.ToString();
                this.tbSANIIRINeed.Text = canalState.SANIIRINeed.ToString();
                this.tbChannelQuantity.Text = canalState.ChannelQuantity.ToString();
                this.tbChannelNeed.Text = canalState.ChannelNeed.ToString();
                this.tbRailNeed.Text = canalState.RailNeed.ToString();
                this.tbShieldNeed.Text = canalState.ShieldNeed.ToString();
                this.tbBridgeNeed.Text = canalState.BridgeNeed.ToString();
                tbModernEquipment.Text = canalState.ModernEquipment.Value.ToString();
                tbEquipmentNeed.Text = canalState.EquipmentNeed.Value.ToString();
                chbAVPBalance.Checked = canalState.BalanceAVP;
            }
            else
            {
                tbBridgeNeed.Text = tbChannelNeed.Text = tbChannelQuantity.Text = tbEquipmentNeed.Text =
                    tbModernEquipment.Text = tbRailNeed.Text = tbSANIIRINeed.Text = tbSANIIRIQuantity.Text = tbShieldNeed.Text =
                    tbWeirNeed.Text = tbWeirQuantity.Text = string.Empty;
                chbAVPBalance.Checked = false;
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);

                cbCanal.Enabled = true;

                cbCanal.SelectedIndexChanged -= cbCanal_SelectedIndexChanged;
                cbCanal.DataSource = DBClass.Canals
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new
                    {
                        ID = se.ID,
                        Name = se.Name
                    }).ToArray();
                cbCanal.DisplayMember = "Name";
                cbCanal.ValueMember = "ID";
                cbCanal.SelectedIndex = -1;
                cbCanal.Text = string.Empty;
                cbCanal.SelectedIndexChanged += new EventHandler(cbCanal_SelectedIndexChanged);
            }
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            regID = Convert.ToInt32(cbRegion.SelectedValue);
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.Enabled = true;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (canalID > 0 && kv >= 0 && year > 0)
            {
                decimal weirQ, weirN, SAN_Q, SAN_N, chanQ, chanN, railN, shieldN, brigeN, badLength, modernEq, eqN;
                bool balaceAVP = chbAVPBalance.Checked, correct = tbBridgeNeed.Text.GetDecimalOrDefault(out brigeN);
                correct &= tbChannelNeed.Text.GetDecimalOrDefault(out chanN);
                correct &= tbChannelQuantity.Text.GetDecimalOrDefault(out chanQ);
                correct &= tbEquipmentNeed.Text.GetDecimalOrDefault(out eqN);
                correct &= tbModernEquipment.Text.GetDecimalOrDefault(out modernEq);
                correct &= tbRailNeed.Text.GetDecimalOrDefault(out railN);
                correct &= tbSANIIRINeed.Text.GetDecimalOrDefault(out SAN_N);
                correct &= tbSANIIRIQuantity.Text.GetDecimalOrDefault(out SAN_Q);
                correct &= tbShieldNeed.Text.GetDecimalOrDefault(out shieldN);
                correct &= tbWeirNeed.Text.GetDecimalOrDefault(out weirN);
                correct &= tbWeirQuantity.Text.GetDecimalOrDefault(out weirQ);

                if (!correct)
                    if (MessageBox.Show(msgClass.msgUncorrectData_Continue, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                if (canalState != null)
                {
                    canalState.BalanceAVP = balaceAVP;
                    canalState.BridgeNeed = brigeN;
                    canalState.ChannelNeed = chanN;
                    canalState.ChannelQuantity = chanQ;
                    canalState.EquipmentNeed = eqN;
                    canalState.ModernEquipment = modernEq;
                    canalState.RailNeed = railN;
                    canalState.SANIIRINeed = SAN_N;
                    canalState.SANIIRIQuantity = SAN_Q;
                    canalState.ShieldNeed = shieldN;
                    canalState.WeirNeed = weirN;
                    canalState.WeirQuantity = weirQ;
                }
                else
                {
                    canalState = new tb_CanalState
                    {
                        BadLength = 0,
                        BalanceAVP = balaceAVP,
                        BridgeNeed = brigeN,
                        CanalID = canalID,
                        ChannelNeed = chanN,
                        ChannelQuantity = chanQ,
                        Date = new DateTime(year, (kv * 3 + 1), 1),
                        EquipmentNeed = eqN,
                        ModernEquipment = modernEq,
                        RailNeed = railN,
                        SANIIRINeed = SAN_N,
                        SANIIRIQuantity = SAN_Q,
                        ShieldNeed = shieldN,
                        WeirNeed = weirN,
                        WeirQuantity = weirQ
                    };
                    DBClass.CanalState.InsertOnSubmit(canalState);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbCanal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCanal.SelectedIndex >= 0)
            {
                canalID = Convert.ToInt32(cbCanal.SelectedValue);
                if (canalID > 0 && kv >= 0 && year > 0)
                    ShowCanalState();
            }
        }

        private void cbKvartal_SelectedIndexChanged(object sender, EventArgs e)
        {
            kv = cbKvartal.SelectedIndex;
            if (canalID > 0 && kv >= 0 && year > 0)
                ShowCanalState();
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            year = Convert.ToInt32(cbYear.Text);
            if (canalID > 0 && kv >= 0 && year > 0)
                ShowCanalState();
        }

    }
}
